#include <stm32h7xx_hal.h>


extern uint32_t boot_param_start;
extern void CPU_Init(void);

typedef void(*F_Void_Void)(void);

typedef struct 
{
    const void *AppAddr;
    
} T_BootParam;


int main(void)
{
    // 初始化CPU
    CPU_Init();
    HAL_RCC_DeInit();
    
    // 关闭全局中断
    __set_PRIMASK(1);
    
    // 关闭滴答定时器
    SysTick->CTRL = 0;
    SysTick->LOAD = 0;
    SysTick->VAL = 0;
    
    // 关闭所有中断，清除中断挂起标志
    for(int i = 0 ; i < 8 ; i++) {
        NVIC->ICER[i] = 0xFFFFFFFF;
        NVIC->ICPR[i] = 0xFFFFFFFF;
    }
    
    // 打开全局中断
    __set_PRIMASK(0);
    
    // 跳转到应用程序
    const T_BootParam *BootParam = (const T_BootParam *)&boot_param_start;
    uint32_t JumpAddress = *(volatile uint32_t*)(BootParam->AppAddr + 4);   // 取中断向量表中的复位中断地址
    F_Void_Void Fun_Application = (F_Void_Void)JumpAddress;                 // 该地址强置为跳转函数地址
    __set_MSP(*(__IO uint32_t*)BootParam->AppAddr);
    Fun_Application();
    
    // 跳转失败执行重启
    NVIC_SystemReset();
}
